//
//  FavoritsController.m
//  FinApp
//
//  Created by Marcel Arbó Lack on 12/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "FavoritsController.h"
#import "ListFlat.h"

//Defines for favourites in UserDefaults
#define kFavouritesList     @"favouritesList"
#define kFlatIdentifier     @"idPis"


@implementation FavoritsController

+ (NSMutableArray *)retrieveAllFavourites {
    NSMutableArray *favouritesFullArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:kFavouritesList]];
    
    NSMutableArray *favouritesArray = [[[NSMutableArray alloc] init] autorelease];
    
    for (NSDictionary *aFullFavourite in favouritesFullArray) 
    {
        // verify if aFullFavorite is a valid array
        if ([aFullFavourite count]!=0)
        {
            ListFlat *aFlat = [ListFlat listFlatFromDictionary:aFullFavourite];
            [favouritesArray addObject:aFlat];
        }
        else    // if aFullFavorite is not a valid array arase it from [[NSUserDefaults standardUserDefaults] objectForKey:kFavouritesList]
        {
            //Done in addFavourite method. Dont allow to add the flat if is not valid.
        }
    }
    
    return favouritesArray;
}

+ (BOOL)isFavourite:(NSInteger)favouriteID {
    
    NSMutableArray *favouritesList = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:kFavouritesList]];
    for (NSDictionary *aFavourite in favouritesList) 
    {
        if ([[aFavourite objectForKey:kFlatIdentifier] intValue] == favouriteID) 
        {
            //The favourtie already exist
            [favouritesList release];
            return YES;
        }
    }
    [favouritesList release];
    return NO;
}

+ (void)addFavouriteToListWithFlat:(NSDictionary *)flatDict {
    
    //Control of is valid favourite flat
    if (flatDict) {
        if ( [flatDict count]>0 ) {
            
            NSMutableArray *favouritesList = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:kFavouritesList]];
            
            //Add newFavourite to favouritesList
            [favouritesList addObject:flatDict];
            
            //Set favouritesList to UserDefaults
            [[NSUserDefaults standardUserDefaults] setObject:favouritesList forKey:kFavouritesList];
            [favouritesList release];
            //Syncronize NSUserDefaults
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}

+ (void)deleteFavourite:(NSInteger)favouriteID {
    NSMutableArray *favouritesList = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:kFavouritesList]];
    for (NSDictionary *aFavourite in favouritesList) {
        if ([[aFavourite objectForKey:kFlatIdentifier] intValue] == favouriteID) {
            //Delete favourite from favouritesList
            [favouritesList removeObject:aFavourite];
            break;
        }
    }
    
    //Set favouritesList to UserDefaults
    [[NSUserDefaults standardUserDefaults] setObject:favouritesList forKey:kFavouritesList];
    //Syncronize NSUserDefaults
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [favouritesList release];
}

@end
